import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/scheduler/ticker.dart';
import 'package:flutter_app/stateLearn/click_txt.dart';
import 'package:http/http.dart' as http;

class AsyncLoad extends StatefulWidget {
  const AsyncLoad({Key? key}) : super(key: key);

  @override
  _AsyncLoadState createState() => _AsyncLoadState();
}

class _AsyncLoadState extends State<AsyncLoad>
    with WidgetsBindingObserver, SingleTickerProviderStateMixin {
  bool showList = true;
  List widgets = [];
 late AnimationController controller;
 late CurvedAnimation curve;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addObserver(this);
    controller = AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    curve = CurvedAnimation(parent: controller, curve: Curves.easeIn);
  }

  @override
  void dispose() {
    WidgetsBinding.instance?.removeObserver(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: (context, widget) {
        return Scaffold(
          body: createGestureDetector(),
        );
      },
    );
  }

  createWidgets() {
    return Container(
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [
          Text("创建文本"),
          Text("再次创建文本"),
        ],
      ),
    );
  }

  //网络请求时的动画过程
  showOrGoneList() {
    if (showList) {
      return Expanded(
        child: Container(
          child: ListView.builder(
            itemBuilder: (context, position) {
              return getRow(position);
            },
            itemCount: widgets.length,
          ),
        ),
      );
    } else {
      return const Center(child: CircularProgressIndicator());
    }
  }

  //显示列表数据或者跳转第二个页面
  createAsyncPage() {
    return Container(
      alignment: Alignment.center,
      child: Column(
        children: [createJump(), createGetData(), showOrGoneList()],
      ),
    );
  }

  //跳转到第二个页面
  createJump() {
    return Padding(
      padding: const EdgeInsets.all(18),
      child: ClickTxt(
        info: "跳转到第二个页面",
        onPressed: () {
          Navigator.pushNamed(context, 'navigatorLearn');
        },
        style: const TextStyle(fontSize: 24),
      ),
    );
  }

  //显示网络请求的list
  createGetData() {
    return ClickTxt(
        info: "获取数据",
        onPressed: () {
          loadData();
          setState(() {
            showList = false;
          });
        },
        style: const TextStyle(fontSize: 24));
  }

  //手势测试
  createGestureDetector() {
    return Container(
      alignment: Alignment.center,
      child: GestureDetector(
        child: RotationTransition(
          turns: curve,
          child: const FlutterLogo(
            size: 200.0,
          ),
        ),
        onDoubleTap: () {
          if (controller.isCompleted) {
            controller.reverse();
          } else {
            controller.forward();
          }
        },
      ),
    );
  }

  //list使用测试
  createScrollView() {
    return ListView(
      children: const [
        Text('Row One'),
        Text('Row Two'),
        Text('Row Three'),
        Text('Row Four'),
        Text('Row Five'),
        Text('Row six'),
      ],
      scrollDirection: Axis.vertical,
    );
  }

  void loadData() async {
    var dataURL = "https://jsonplaceholder.typicode.com/posts";
    var future = await http.get(Uri.parse(dataURL));
    setState(() {
      showList = true;
      widgets = json.decode(future.body);
    });
  }

  Widget getRow(int position) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: Text("Row ${widgets[position]["title"]}"),
    );
  }
}
